import json
import time

import redis
from scrapy.utils.project import get_project_settings

setting = get_project_settings()
REDIS_HOST = setting.get('REDIS_HOST')
REDIS_PORT = setting.get('REDIS_PORT')
REDIS_DB = setting.get('REDIS_DB')
REDIS_PWD = setting.get('REDIS_PWD')
PROXY_POOL = setting.get('PROXY_POOL')
PROXY_POOL_ERROR = setting.get('PROXY_POOL_ERROR')
PROXY_POOL_SOCKS5 = setting.get('PROXY_POOL_SOCKS5')

ERROR_TIME_LIMIT = setting.get('ERROR_TIME_LIMIT')
PROXIES_LIST = setting.get('PROXIES_LIST')
PROXIES_LIST_SOCKS5=setting.get('PROXIES_LIST_SOCKS5')

class Proxy:
    """
    适用于另一个代理平台的代理处理类
    """

    def __init__(self):
        self.r = redis.Redis(host=REDIS_HOST, port=REDIS_PORT, db=REDIS_DB, password=REDIS_PWD, decode_responses=True)

    def get_proxy(self, pool_name=PROXY_POOL):
        while 1:
            proxy_ = self.r.lpop(pool_name)

            if proxy_:
                return json.loads(proxy_)
            else:
                print("代理池 : {} 中暂无代理可用".format(PROXY_POOL))
                time.sleep(2)

    def add(self, proxys, pool_name=PROXY_POOL):
        for i in proxys:
            proxy_item = {'proxy': i, 'error_time': 0}
            proxy_item = json.dumps(proxy_item)
            self.r.lpush(pool_name, proxy_item)

    def back(self, proxy_item):
        # 代理使用完后 调用该方法  异常次数大于 限定量的  进异常队列
        error_time = proxy_item['error_time']
        proxy = proxy_item['proxy']
        if error_time > ERROR_TIME_LIMIT:
            self.to_error_pool(proxy)
        else:
            self.r.lpush(PROXY_POOL, json.dumps(proxy_item))

    def to_error_pool(self, proxy_):
        print("本代理失败次数过多:{}".format(proxy_))
        self.r.lpush(PROXY_POOL_ERROR, proxy_)


if __name__ == '__main__':
    pp = Proxy()
    for i in range(20):
        pp.add(PROXIES_LIST,PROXY_POOL)
    for i in range(20):
        pp.add(PROXIES_LIST_SOCKS5,PROXY_POOL_SOCKS5)
    # prox = pp.get_proxy()
    # prox['error_time'] = 16

    # pp.back(prox)
